<?php
App::uses('AppController', 'Controller');

class CitiesController extends AppController {

      
        public $components = array(
            'Search.Prg' => array(
                'presetForm' => array(
                    'paramType' => 'querystring',
                ),
                'commonProcess' => array(
                    'paramType' => 'querystring',
                    'filterEmpty' => true,
                ),
            ),
        );

      
        public $uses = array('City');
        public $paginate = array(
        'limit' => 6,
        'order' => array(
            'City.id' => 'asc'
        )
        );
        public $presetVars = true;
        public $limit_countries = array();

        public function beforeFilter() {
                parent::beforeFilter();
                if (in_array($this->request->action, array('admin_toggle', 'loadCity'))) {
                        $this->Security->unlockedActions = $this->request->action;
                }
                if (in_array($this->request->action, array('admin_index', 'admin_add', 'admin_edit'))) {
                        $this->limit_countries = json_decode(Configure::read('Country'));
                }
        }

       
      
        public function admin_index() {
             $this->set('title_for_layout', __('Cities'));
                
                $this->Prg->commonProcess();
                $this->paginate = array(
                //'conditions' => $this->City->parseCriteria($this->passedArgs),
                'limit' => 20,
                'order' => array(
                        'City.id' => 'asc'
                     )
                   );

                //$this->City->recursive = 0;
                //$this->paginate['City']['order'] = 'City.weight ASC';
               // $this->set('cities', $this->paginate($this->City->parseCriteria($this->request->query)));
                $this->set('cities', $this->City->find('all'));
                //$this->set('displayFields', $this->City->displayFields());
                //$this->set('searchFields', $searchFields);
                
               $City = $this->paginate('City');
               $this->set('cities', $City);
                    
               
                }  
       public function admin_add() {
                $this->set('title_for_layout', __('Add City'));

                if (!empty($this->request->data)) {                     
                        $this->City->create();
                        if ($this->City->save($this->request->data)) {
                                $this->Session->setFlash(__('The City has been saved'), 'default', array('class' => 'success'));
                                $this->redirect(array('action' => 'edit', $this->City->id));
                        } else {
                                $this->Session->setFlash(__('The City could not be saved. Please, try again.'), 'default', array('class' => 'error'));
                        }
                }
               
        }   

         public function admin_edit($id = null) {
                $this->set('title_for_layout', __('Edit City'));

                if (!$id && empty($this->request->data)) {
                        $this->Session->setFlash(__('Invalid City'), 'default', array('class' => 'error'));
                        $this->redirect(array('action' => 'index'));
                }

                if (!empty($this->request->data)) {
                        if ($this->City->save($this->request->data)) {
                                $this->Session->setFlash(__('The City has been saved'), 'default', array('class' => 'success'));
                                $this->redirect(array('action' => 'edit', $this->City->id));
                        } else {
                                $this->Session->setFlash(__('The City could not be saved. Please, try again.'), 'default', array('class' => 'error'));
                        }
                }
                if (empty($this->request->data)) {
                        $this->request->data = $this->City->read(null, $id);
                }
               
        }

        /**
         * Admin delete
         *
         * @param integer $id
         * @return void
         * @access public
         */
        public function admin_delete($id = null) {
                if (!$id) {
                        $this->Session->setFlash(__('Invalid id for City'), 'default', array('class' => 'error'));
                        $this->redirect(array('action' => 'index'));
                }
                if ($this->City->delete($id)) {
                        $this->Session->setFlash(__('City deleted'), 'default', array('class' => 'success'));
                        $this->redirect(array('action' => 'index'));
                }
        }
 
        
}
